﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XNAProjectLibrary;

namespace XNAProject
{
    public sealed class FloatMenuState : BaseGameState, IFloatMenuState
    {
        private Texture2D texture;
        private SpriteFont font;
        private int selected;

        private string[] entries = 
            {     
                "Resume Game",
                "Options",                
                "Exit Game"
            };                                                                                          //menuitems

        public FloatMenuState(Game game)
            : base(game)
        {
            game.Services.AddService(typeof(IFloatMenuState), this);                                    //register service
        }

        public override void Update(GameTime gameTime)
        {
            if(Input.WasPressed(0, Buttons.Start, Keys.Escape))                                         //input handling
            {
                GameManager.PopState();                                                                 //pop this state
            }

            if (Input.WasPressed(0, Buttons.DPadUp, Keys.Up))                                           //up
            {
                selected--;
            }
            if (Input.WasPressed(0, Buttons.DPadDown, Keys.Down))                                       //down
            {
                selected++;
            }

            if (selected < 0)                                                                           //loop
                selected = entries.Length - 1;
            if (selected == entries.Length)
                selected = 0;

            if (Input.WasPressed(0, Buttons.Start, Keys.Enter) ||
                (Input.WasPressed(0, Buttons.A, Keys.Space)))
            {                                                                                           //handle selection
                switch (selected)
                {
                    case 0:                                                                             //Resume Game
                        {
                            GameManager.PopState();
                            break;
                        }
                    case 1:                                                                             //options menu
                        {
                            OurGame.PlayState.UnloadComponents();
                            OurGame.PrepareState.UnloadComponents();
                            GameManager.ChangeState(OurGame.OptionsMenuState.Value);
                            break;
                        }
                    case 2:                                                                             //Exit Game
                        {
                            OurGame.PlayState.UnloadComponents();
                            GameManager.ChangeState(OurGame.StartMenuState.Value);
                            break;
                        }
                }
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Vector2 pos = new Vector2((GraphicsDevice.Viewport.Width - texture.Width) / 2,              //drawing pos
                (GraphicsDevice.Viewport.Height - texture.Height) / 2);
            Vector2 position = new Vector2(pos.X + 90, pos.Y + texture.Height / 2 - 30);

            OurGame.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
            OurGame.spriteBatch.Draw(texture, pos, Color.White);

            for (int i = 0; i < entries.Length; i++)
            {
                Color color;
                float scale;

                if (i == selected)
                {
                    // The selected entry is yellow, and has an animating size.
                    double time = gameTime.TotalGameTime.TotalSeconds;

                    float pulsate = (float)Math.Sin(time * 12) + 1;

                    color = Color.White;
                    scale = 1 + pulsate * 0.05f;
                }
                else
                {
                    // Other entries are white.
                    color = Color.Blue;
                    scale = 1;
                }
                // Draw text, centered on the middle of each line.
                Vector2 origin = new Vector2(0, font.LineSpacing / 2);
                Vector2 shadowPosition = new Vector2(position.X - 2, position.Y - 2);

                //Draw Shadow
                OurGame.spriteBatch.DrawString(font, entries[i],
                    shadowPosition, Color.Black, 0, origin, scale, SpriteEffects.None, 0);
                //Draw Text
                OurGame.spriteBatch.DrawString(font, entries[i],
                    position, color, 0, origin, scale, SpriteEffects.None, 0);

                position.Y += font.LineSpacing;
            }
            OurGame.spriteBatch.End();


            base.Draw(gameTime);
        }

        protected override void LoadContent()
        {
            texture = Content.Load<Texture2D>(@"Textures\Menu\floatMenu");                                   //load texture, font
            font = Content.Load<SpriteFont>(@"Fonts\menu");
            base.LoadContent();
        }
    }
}
